package com.shungen.apicloud.controller;

import com.shungen.apicloud.common.BaseResponse;
import com.shungen.apicloud.common.ErrorCode;
import com.shungen.apicloud.common.ResultUtils;
import com.shungen.apicloud.exception.BusinessException;
import com.shungen.apicloud.exception.ThrowUtils;
import com.shungen.apicloud.model.dto.email.EmailCodeRequest;
import com.shungen.apicloud.service.EmailService;
import com.shungen.apicloud.utils.EmailValidator;
import com.shungen.apicloud.utils.RedisUtil;
import com.shungen.apicloud.utils.VerificationCodeGenerator;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.mail.MessagingException;

/**
 * @author shungen
 * @date 2024年07月23日 16:55
 */
@RestController
@RequestMapping("/email")
public class EmailController {

    @Resource
    private EmailService emailService;

    @Resource
    private RedisUtil redisUtil;

    @PostMapping("/sendCode")
    public BaseResponse<Boolean> sendCode(@RequestBody EmailCodeRequest emailCodeRequest) {
        try {
            // 检查参数
            ThrowUtils.throwIf(!EmailValidator.isValidEmail(emailCodeRequest.getTo()), ErrorCode.PARAMS_ERROR, "邮箱格式不合法");

            String subject = "API-Cloud";

            String code = VerificationCodeGenerator.generateCode(6);
            String content = getContent(code);
            boolean addResult = redisUtil.set(emailCodeRequest.getTo(), code, 60);
            ThrowUtils.throwIf(!addResult,ErrorCode.SYSTEM_ERROR,"系统异常");
            emailService.sendEmail(emailCodeRequest.getTo(), subject, content);
            return ResultUtils.success(true);
        } catch (MessagingException e) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "邮件发送失败!");
        }
    }


    private String getContent(String code){
        String company = "啊哈哈团队";
        String content= "<!DOCTYPE html>\n" +
                "<html>\n" +
                "<head>\n" +
                "<title></title>\n" +
                "<style>\n" +
                ".qmbox {\n" +
                "\tpadding: 0;\n" +
                "}\n" +
                ".qm_con_body_content {\n" +
                "\theight: auto;\n" +
                "\tmin-height: 100px;\n" +
                "\t_height: 100px;\n" +
                "\tword-wrap: break-word;\n" +
                "\tfont-size: 14px;\n" +
                "\tfont-family: \"lucida Grande\", Verdana, \"Microsoft YaHei\";\n" +
                "}\n" +
                ".body {\n" +
                "\tline-height: 170%;\n" +
                "}\n" +
                "BODY {\n" +
                "\tfont-family: \"lucida Grande\", Verdana, \"Microsoft YaHei\";\n" +
                "\tfont-size: 12px;\n" +
                "\t-webkit-font-smoothing: subpixel-antialiased;\n" +
                "}\n" +
                "BODY {\n" +
                "\tmargin: 0;\n" +
                "\tpadding: 0;\n" +
                "}\n" +
                "BODY {\n" +
                "\tbackground-color: #fff;\n" +
                "\tfont-size: 12px;\n" +
                "}\n" +
                "BODY {\n" +
                "\tbackground: #fff;\n" +
                "}\n" +
                "BODY {\n" +
                "\tbackground: #fff;\n" +
                "\tcolor: #000;\n" +
                "\tfont-weight: normal;\n" +
                "\tfont-family: \"lucida Grande\", Verdana, \"Microsoft YaHei\";\n" +
                "\tpadding: 0 7px 6px 4px;\n" +
                "\tmargin: 0;\n" +
                "}\n" +
                "HTML {\n" +
                "\ttop: 0px;\n" +
                "}\n" +
                ".body P {\n" +
                "\tline-height: 170%;\n" +
                "}\n" +
                "</style>\n" +
                "</head>\n" +
                "\n" +
                "<BODY mu=\"mu\" md=\"md\" module=\"qmReadMail\" context=\"ZC1912-rQ7uXSy7P7HThxdLFCOuY92\"><DIV class=mailcontainer id=qqmail_mailcontainer>\n" +
                "<DIV id=mainmail style=\"MARGIN-BOTTOM: 12px; POSITION: relative; Z-INDEX: 1\">\n" +
                "<DIV class=body id=contentDiv style=\"FONT-SIZE: 14px; HEIGHT: auto; POSITION: relative; ZOOM: 1; PADDING-BOTTOM: 10px; PADDING-TOP: 15px; PADDING-LEFT: 15px; Z-INDEX: 1; LINE-HEIGHT: 1.7; PADDING-RIGHT: 15px\" onmouseover=getTop().stopPropagation(event); onclick=\"getTop().preSwapLink(event, 'html', 'ZC1912-rQ7uXSy7P7HThxdLFCOuY92');\">\n" +
                "<DIV id=qm_con_body>\n" +
                "<DIV class=\"qmbox qm_con_body_content qqmail_webmail_only\" id=mailContentContainer>\n" +
                "<DIV class=main style=\"OVERFLOW: hidden; WIDTH: 100%; BACKGROUND-COLOR: #f7f7f7\">\n" +
                "<DIV class=content style=\"BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; BACKGROUND: #ffffff; BORDER-BOTTOM: #cccccc 1px solid; PADDING-BOTTOM: 10px; PADDING-TOP: 10px; PADDING-LEFT: 25px; BORDER-LEFT: #cccccc 1px solid; MARGIN: 50px; PADDING-RIGHT: 25px\">\n" +
                "<DIV class=header style=\"MARGIN-BOTTOM: 30px\">\n" +
                "<P>亲爱的用户：</P></DIV>\n" +
                "<P>您好！您正在进行邮箱验证，本次请求的验证码为：</P>\n" +
                "<P><SPAN style=\"FONT-SIZE: 18px; FONT-WEIGHT: bold; COLOR: #f90\">"+code+"</SPAN><SPAN style=\"COLOR: #000000\">(为了保障您帐号的安全性，请在10分钟内完成验证)</SPAN></P>\n" +
                "<DIV class=footer style=\"MARGIN-TOP: 30px\">\n" +
                "<P>"+company+"</P>\n" +
                "<P><SPAN style=\"BORDER-BOTTOM: #ccc 1px dashed; POSITION: relative; _display: inline-block\" t=\"5\" times=\"\" isout=\"0\">2019-02-12</SPAN></P></DIV>\n" +
                "<DIV class=tip style=\"COLOR: #cccccc; TEXT-ALIGN: center\">该邮件为系统自动发送，请勿进行回复 </DIV></DIV></DIV></DIV></DIV></DIV></DIV></DIV></BODY>\n" +
                "</html>\n";

        return content;
    }
}
